package com.jlz.actions.console.category;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Standard;
import org.springframework.beans.factory.annotation.Autowired;

import com.jlz.beans.core.CategoryBean;
import com.jlz.managers.core.Categories;
import com.julewa.Client;
import com.julewa.db.BeanValidator;

@Standard(rest="category.ID")
public class SaveAction {
	private @Autowired Client client;
	private @Autowired Categories categories;
	//由spring保证生成一个新实例!
	private @Autowired CategoryBean category;
	private @Autowired BeanValidator validator;
	//
	public String execute(){
		if(!validator.checkColumns("category", category)){
			return "edit";
		};
		if(category.getID() == null){
			categories.append(category);
		}else{
			categories.update(category);
		}
		client.putMessage("成功保存");
		return "edit";
	}
	
	@Action("update")
	public String update(){
		if(category.getID() != null){
			CategoryBean c = categories.get(category.getID());
			if(c == null) category.setID(null);
			else category = c;
		}
		return "edit";
	}
	
	@Action("create")
	public String create(){
		return "edit";
	}
	
	public CategoryBean getCategory(){
		return category;
	}
	
}
